/**
 * SwitchImg
 * 简单切换图片类
 */
define('utils/SwitchImg', null, function() {

function SwitchImg() {
    this.imgWrapper = null;
    this.imgs = null;
    this.titleObj = null;
    this.handlers = [];
    this.idx = 0;  // 当前图片索引
    this.t = 6000;  // 6秒后自动执行
    this.closeClass = '';
    this.openClass = 'active';
}
SwitchImg.prototype = {
    constructor : SwitchImg
    ,closeAllBg : function() {
        var len = this.handlers.length;
        for(var i=0; i<len; i++) {
            this.handlers[i].className = this.closeClass;
        }
    }
    ,closeAllImg: function() {
        var len = this.handlers.length;
        for(var i=0; i<len; i++) {
            this.imgs[i].style.display = 'none';
        }
    }
    ,autoExecute : function() {
        var _self = this;
        if(this.idx == this.handlers.length) {this.idx = 0;}
        
        if(1 === this.handlers.length) {
            return;
        }
        
        setTimeout(function(){
            _self.closeAllBg();
            _self.firstBoot(_self.idx);
            
        }, _self.t);
    }
    ,firstBoot : function(x) {
        // 当前背景打开
        this.handlers[x].className = this.openClass;
        // 更改图片
        this.closeAllImg();
        this.imgs[x].style.display = 'block';
        // 更改标题
        this.titleObj.innerHTML = this.handlers[x].getAttribute('data-title');
        
        this.idx = ++x;
        this.autoExecute();
    }
    ,start : function() {
        var _self = this;
        var len = _self.handlers.length;
        // 第一次加载处理
        _self.firstBoot(0);
        
        for(var i=0; i<len; i++) {
            _self.handlers[i].onmouseover = function(){
                // 关闭所有背景
                _self.closeAllBg();
                // 当前背景打开
                this.className = _self.openClass;
                // 更改图片
                _self.closeAllImg();
                _self.imgs[this.getAttribute('data-index')].style.display = 'block';
                // 更改标题
                _self.titleObj.innerHTML = this.getAttribute('data-title');
            };
        }
    }
    ,init : function(imgWrapper, titleObj, handlers) {
        this.imgWrapper = imgWrapper;
        this.imgs = imgWrapper.getElementsByTagName('a');
        this.titleObj = titleObj;
        this.handlers = handlers;
        
        this.start();
    }
};

return SwitchImg;

});


